﻿using Microsoft.Extensions.DependencyInjection;
using MingYuanYun.Context.Abstractions;
using MingYuanYun.StructuredLog.Abstractions;

namespace ControlCenter.ApplicationService
{
    /// <summary>
    /// 应用服务基类
    /// </summary>
    public class ApplicationServiceBase
    {
        private IMyLogger _logger; 

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context"></param>
        public ApplicationServiceBase(IServiceContext context)
        {
            this.ServiceContext = context;
        }

        protected IServiceContext ServiceContext { get; }

        protected IMyLogger Logger => this._logger ?? (this._logger = this.GetService<IMyLogger>());
         
         

        /// <summary>
        /// Get the config section in json file and deserialize to class.
        /// </summary>
        protected T GetConfig<T>(string sectionName)
        {
            return this.ServiceContext.GetConfig<T>(sectionName);
        }

        /// <summary>
        /// Get the injected service, if the service doesn't inject at startup, it will throw exception.
        /// </summary>
        protected T GetService<T>()
        {
            return this.ServiceContext.GetService<T>();
        }

        /// <summary>
        /// Create the specific scope for committing transaction.
        /// </summary>
        /// <returns></returns>
        protected IServiceScope CreateScope()
        {
            return this.ServiceContext.CreateScope();
        }
    }
}
